//
//  FindViewController.m
//  B段_开眼
//
//  Created by hu on 16/4/19.
//  Copyright © 2016年 Howie. All rights reserved.
//

#import "FindViewController.h"
#import "SDCycleScrollView.h"
#import <SDWebImage/UIImageView+WebCache.h>
@interface FindViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, SDCycleScrollViewDelegate>

// 解析之后 -  数组
@property (nonatomic, strong) NSMutableArray *dataArr;


// 缩略图的大小
#define iMAGEWIDTH  self.view.frame.size.width / 2

// CollectionView
@property (nonatomic, strong) UICollectionView *collView;


@property (nonatomic, strong) YIem_SideslipViewController *sides;


/**
 *  优质作者
 */
@property (nonatomic, strong) UIView *author;
@property (nonatomic, strong) UILabel *youLabel;
@property (nonatomic, strong) UIButton  *dianbutton;
@property (nonatomic, strong) UILabel *dianLabel;
@property (nonatomic, strong) UIButton *kanButton;


// 轮播图
@property (nonatomic, strong) SDCycleScrollView *scroll;
@property (nonatomic, strong)  NSMutableArray *arrTU;
@property (nonatomic, strong) NSMutableArray *arrURL;
@end

@implementation FindViewController


- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self GetDataBase];
    self.dataArr = [NSMutableArray array];
    self.arrTU = [NSMutableArray array];
    self.arrURL = [NSMutableArray array];
    
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"YIem";
    
    
    
    
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(iMAGEWIDTH - 2, iMAGEWIDTH - 2);
    layout.minimumInteritemSpacing = 1;
    layout.minimumLineSpacing = 2;
    layout.sectionInset = UIEdgeInsetsMake(1, 1, 1, 1);
    
    self.collView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
    self.collView.contentInset = UIEdgeInsetsMake(self.collView.frame.size.height / 1.5, 0, 0, 0);
    self.collView.backgroundColor = [UIColor whiteColor];
    self.collView.delegate = self;
    self.collView.dataSource = self;
    self.collView.bounces = NO;
    [self.view addSubview:self.collView];
    
    [self.collView registerClass:[YIem_FindCollectionViewCell class] forCellWithReuseIdentifier:@"YIem_FindCollectionViewCell"];
    
    self.sides = [[YIem_SideslipViewController alloc] init];
    self.sides.view.frame = CGRectMake(- self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self addChildViewController:self.sides];
    [self.view addSubview:self.sides.view];
    UIScreenEdgePanGestureRecognizer *screenGR = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenAction:)];
    screenGR.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:screenGR];
    
// >>>>>>>******************************
    /**
     *  头部视图
     *
     */
    UIView *top = [[UIView alloc] initWithFrame:CGRectMake(0, -(self.collView.frame.size.height / 1.5), self.view.frame.size.width, self.collView.frame.size.height / 1.5)];
    
//    top.backgroundColor = [UIColor colorWithRed:1.0 green:0.9242 blue:0.807 alpha:1.0];
    [self.collView addSubview:top];
    // ScorellView
    self.scroll = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, top.frame.size.width, top.frame.size.height / 2) delegate:self placeholderImage:[UIImage imageNamed:@"1.pic_hd"]];
    
    self.scroll.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;

    self.scroll.currentPageDotColor = [UIColor cyanColor];
    [top addSubview:self.scroll];

    
  
    
// >>>>>>>******************************
    /**
     *  优质作者
     */

    self.author = [[UIView alloc] initWithFrame:CGRectMake(0, (top.frame.origin.y + top.frame.size.height) / 2 + (top.frame.size.height / 2), top.frame.size.width, top.frame.size.height / 2)];
    self.author.backgroundColor = [UIColor whiteColor];
    [top addSubview:self.author];
    self.youLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.author.frame.size.width, 40)];
    self.youLabel.textAlignment = NSTextAlignmentCenter;
    self.youLabel.font = [UIFont systemFontOfSize:20];
    

   
    
    
    
    
    // 返回
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_back_normal"] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor colorWithRed:0.2077 green:0.2077 blue:0.2077 alpha:1.0];
    // 搜索
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Unknown"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor colorWithRed:0.2077 green:0.2077 blue:0.2077 alpha:1.0];

    
    
  
  
}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSString *str = [self.arrURL objectAtIndex:index];
    if ([str hasPrefix:@"eyepetizer://recommend/"]) {
        YIem_Explore_ViewController *exp = [[YIem_Explore_ViewController alloc] init];
        [exp setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        UINavigationController *NC = [[UINavigationController alloc] initWithRootViewController:exp];
        [self presentViewController:NC animated:YES completion:nil];

        
        NSLog(@"wu");
    }else if ([str hasPrefix:@"eyepetizer://webview/"]) {
        
        YIem_Find_Topics_DetailViewController *detail = [[YIem_Find_Topics_DetailViewController alloc] init];
        detail.strUrl = self.arrURL[index];
        
        [detail setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        UINavigationController *NC = [[UINavigationController alloc] initWithRootViewController:detail];
        [self presentViewController:NC animated:YES completion:nil];
    }else {
        
        NSLog(@"你是SB");
    }
    
}
- (void)rightAction
{
    YIem_Search_ViewController *search = [[YIem_Search_ViewController alloc] init];
    
    [search setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    UINavigationController *NC = [[UINavigationController alloc] initWithRootViewController:search];
    [self presentViewController:NC animated:YES completion:nil];
    
}
- (void)leftAction
{
    YIem_SideslipViewController *sid = [[YIem_SideslipViewController alloc] init];
    [sid setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:sid];
    [self presentViewController: nc animated:YES completion:nil];
}
// 抽屉效果
- (void)screenAction:(UIScreenEdgePanGestureRecognizer *)sender
{
    // 防止一次操作 多次进入
    if (UIGestureRecognizerStateEnded == sender.state) {
//        NSLog(@"出来");
        
        
        [UIView animateWithDuration:1.0 animations:^{
            
//            self.view.frame = CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
            YIem_SideslipViewController *sid = [[YIem_SideslipViewController alloc] init];
            [sid setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
            UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:sid];
            [self presentViewController: nc animated:YES completion:nil];
        }];
        
    }
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.dataArr.count == 0) {
        return 0;
    }
    return [[self.dataArr objectAtIndex:2] count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YIem_FindCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YIem_FindCollectionViewCell" forIndexPath:indexPath];
    
    cell.model = [[self.dataArr objectAtIndex:2] objectAtIndex:indexPath.row];
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    NSLog(@"%ld", indexPath.row);
    
    if (0 == indexPath.row) {
        YIem_Find_Topics_ViewController *top = [[YIem_Find_Topics_ViewController alloc] init];
        [top setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:top];
        [self presentViewController:nc animated:YES completion:nil];
        
    }else if (1 == indexPath.row) {
        
        YIem_Find_FoundViewController *found = [[YIem_Find_FoundViewController alloc] init];
        [found setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        UINavigationController *nc =[[ UINavigationController alloc] initWithRootViewController:found];
        [self presentViewController:nc animated:YES completion:nil];

        
    } else {
        
    
    YIem_FindDetailViewController *findVC = [[YIem_FindDetailViewController alloc] init];
    [findVC setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    findVC.strID = [[self.dataArr objectAtIndex:2][indexPath.row]ID];
    findVC.strTitle = [[self.dataArr objectAtIndex:2][indexPath.row]title] ;
    
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:findVC];
    [self presentViewController:nc animated:YES completion:nil];
        
    }
}





/**
 *  数据解析
 */
- (void)GetDataBase
{
    NSString *url = @"http://baobab.wandoujia.com/api/v3/discovery?";
    [YIem_FindViewModel getDataWithPage:url andUpdate:YES andBlock:^(NSMutableArray *arr) {
       
  
        self.dataArr = arr;
 
        for (NSInteger i = 0 ; i < [[self.dataArr objectAtIndex:0] count]; i++) {
             YIem_FindCellModelOne *model = [[YIem_FindCellModelOne alloc] init];
            model = [[self.dataArr objectAtIndex:0] objectAtIndex:i];
            [self.arrTU addObject:model.image];
            [self.arrURL addObject:model.actionUrl];
        }
        self.scroll.imageURLStringsGroup = self.arrTU;
        
        
        YIem_FindCellModelTwo *model = [[YIem_FindCellModelTwo alloc] init];
        model.title = [[[arr objectAtIndex:1]objectAtIndex:0] title];
        
        self.youLabel.text = [NSString stringWithFormat:@"%@", model.title];
        [self.author addSubview:self.youLabel];
        
        
        
        for (NSInteger i = 0; i < 4; i++) {
            UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake( 20 + i * (self.author.frame.size.width / 5 + 15), (self.youLabel.frame.origin.y + self.youLabel.frame.size.height) + 10, self.author.frame.size.width / 5, self.author.frame.size.width / 5)];

            [img sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[[arr objectAtIndex:1]objectAtIndex:i] icon]]]];
            img.layer.cornerRadius = img.frame.size.width / 2;
            img.layer.masksToBounds = YES;
            [self.author addSubview:img];

            self.dianbutton = [UIButton buttonWithType:UIButtonTypeSystem];
            self.dianbutton.frame = CGRectMake( 20 + i * (self.author.frame.size.width / 5 + 15), (self.youLabel.frame.origin.y + self.youLabel.frame.size.height) + 10, self.author.frame.size.width / 5, self.author.frame.size.width / 5);
//
            [self.dianbutton addTarget:self action:@selector(dianButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            self.dianbutton.tag = 100000 + i;

            [self.author addSubview:self.dianbutton];
            
            self.dianLabel = [[UILabel alloc] initWithFrame:CGRectMake(20 + i * (self.author.frame.size.width / 5 + 15) , self.dianbutton.frame.origin.y + self.dianbutton.frame.size.height, self.author.frame.size.width / 5, 30)];
            self.dianLabel.text = [NSString stringWithFormat:@"%@", [[[arr objectAtIndex:1] objectAtIndex:i]name]];
            self.dianLabel.textAlignment = NSTextAlignmentCenter;
            [self.author addSubview:self.dianLabel];
        }
        self.kanButton = [UIButton buttonWithType:UIButtonTypeSystem];
        self.kanButton.frame = CGRectMake(0, self.dianLabel.frame.size.height + self.dianLabel.frame.origin.y, self.author.frame.size.width,50);
        
        [self.kanButton setTitle:@"查看更多" forState:UIControlStateNormal];
        [self.kanButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        self.kanButton.titleLabel.font = [UIFont systemFontOfSize:20];
        [self.kanButton addTarget:self action:@selector(kanButtonAction) forControlEvents:UIControlEventTouchUpInside];
        [self.author addSubview:self.kanButton];
        
        [self.collView reloadData];
    }];
    
    
    
}

- (void)dianButtonAction:(UIButton *)sender
{
    
    NSInteger index = sender.tag - 100000;
//    NSLog(@"%ld", sender.tag - 100000);
    YIem_Find_Found_Sub_ViewController *sub = [[YIem_Find_Found_Sub_ViewController alloc] init];
    [sub setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
//    NSInteger index = [tap view].tag - 10000;
    sub.model = [[self.dataArr objectAtIndex:1] objectAtIndex:index];
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:sub];
    [self presentViewController:nc animated:YES completion:nil];

}
- (void)kanButtonAction
{
    YIem_Find_FoundViewController *found = [[YIem_Find_FoundViewController alloc] init];
    [found setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    UINavigationController *nc =[[ UINavigationController alloc] initWithRootViewController:found];
    [self presentViewController:nc animated:YES completion:nil];
//    [self.navigationController pushViewController:found animated:YES];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
